#include "GameCore.h"
#include "GameMenu.h"
#include <stdlib.h>
#include "../texture/fractal.h"

using namespace sleek;

namespace interne
{
    void associate(gui::frame*i)
    {
        gui::font *tmp = new gui::font("ttf/Prototype.ttf");
        tmp->setColor(texture::pixel(255,64,0));
        i->setFont(tmp);
    }
    GameMenu::GameMenu(GameCore *co,core::engine::world* eng) : core(co), screen(eng->getDevice()), smgr(eng)
    {
        core::math::vector2du screensize = screen->getInfo().size/2;

        picture  = smgr->getInterface()->addPicture(texture::LoadTextureFromPng("element/background.png"));
        start = smgr->getInterface()->addButton("Start", core::math::aabbox2di(core::math::vector2di(0,0),core::math::vector2di(250,25)));
        quit = smgr->getInterface()->addButton("Quit", core::math::aabbox2di(core::math::vector2di(0,0),core::math::vector2di(250,25)));

        picture->setId(0);
        start->setId(12);
        quit->setId(13);

        associate(picture);
        associate(start);
        associate(quit);

        picture->move(core::math::vector2di(0,0));
        start->move(core::math::vector2di(screensize.width-125,screensize.height-20));
        quit->move(core::math::vector2di(screensize.width-125,screensize.height+20));

        smgr->getInterface()->addCustomFrame(picture);
        smgr->getInterface()->addCustomFrame(start);
        smgr->getInterface()->addCustomFrame(quit);
        smgr->getInterface()->ActiveElement(start);

        screen->setEventReceiver(this);
    }
    GameMenu::~GameMenu()
    {
        smgr->getInterface()->removeFrame(start);
        smgr->getInterface()->removeFrame(quit);
        smgr->getInterface()->removeFrame(picture);
    }
    bool GameMenu::manage(core::device::input *a)
    {
        gui::frame *tmp = a->gui.called;

        if(tmp)
            printf("%s => %d\n", sleek::gui::interface_event_code_name[a->gui.code], tmp->getId());

        switch(a->gui.code)
        {
            case gui::IET_BUTTON_CLICKED:
                switch(tmp->getId())
                {
                    case 12:
                        core->changeState(GMS_GAME);
                    break;
                    case 13:
                        core->changeState(GMS_QUIT);
                    break;
                    default:
                    break;
                }
            break;
        }
        return event::manage(a);
    }
    void GameMenu::render()
    {
    }
}
